package offer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

public class offerII_0083 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result=new LinkedList<>();
        List<Integer> path=new LinkedList<>();
        HashSet<Integer> set=new HashSet<>();
        search(result,path,set,0,nums);
        return result;
    }
    public void search(List<List<Integer>> result,List<Integer> path,HashSet<Integer> set,int num,int[] nums){
        if(num==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<nums.length;++i){
            if(!set.contains(nums[i])){
                path.add(nums[i]);
                set.add(nums[i]);
                search(result, path, set, num+1, nums);
                path.remove(path.size()-1);
                set.remove(nums[i]);
            }
        }
    }
}
